<?php 

	require_once("src/model/ValidateLoginInfo.php"); 
	
	
class LoginForm
{
	/**
	 * @param $errorMessage string
	 * @param $lastUsername string
	 * 
	 * @return string, loginForm.
	 */
	public function GetForm($errorMessage, $lastUsername)
	{		
		return 
		'<div id="loginForm">
			<h2>Ej inloggad </h2>
			  
			<form action="?login"  method="post" id="form1"  >
				<fieldset>
					<legend> Skriv in ditt användarnamn och lösenord! </legend>
					<p>' . $errorMessage . '</p>
					<label for="usernameInput"> Användarnamn:  </label>
					<input id="usernameInput" type="text" name="username" 
				  		value="' . $lastUsername .'" /> 
					
					<label for="userPassInput"> Password: </label>
					<input id="userPassInput" type="text" name="userpassword" value="" /> 
					<label>Håll mig inloggad:
						<input type="checkbox" name="RememberMe" value="RememberMe"/>
					</label>	
					<input type="submit" name="Submit" value="Logga in" />
				</fieldset>
			</form>
		 </div>';
	}	

	/**
	 * @return string, Session - username
	 */
	public function GetUsername()
	{
		if(!empty($_POST['username']))
		{
			return $_POST['username'];
		}
		else {
			return"";
		}
	}
	
	/**
	 * @return string, Session - userpassword
	 */
	public function GetUserPassword()
	{
		if(!empty($_POST['userpassword']))
		{
			return $_POST['userpassword'];
		}
		else 
		{
			return "";
		}
	}
	
	/**
	 * @Return boolean. True if the user wants us to 
	 * remember them.
	 */
	public function RememberMe()
	{	
		if(isset($_POST['RememberMe']))
		{
			if($_POST['RememberMe'] == "RememberMe")
			{
				if(!$this->AreLoginCookiesSet())
				{ 	
					$endtime = time()+30;
					file_put_contents("endtime.txt", $endtime);
					
					setcookie("username", $this->GetUsername(), $endtime);
					setcookie("userpassword", md5($this->GetUserPassword()), $endtime);
					
				}
				return true;
			}	
		}
		return false;
	}
	
	/**
	 * @return boolean, true if login Cookies are set.
	 */
	public function AreLoginCookiesSet()
	{
		if(isset($_COOKIE['username'])&& $_COOKIE['userpassword'])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * @return boolean
	 */
	public function CookiesExistsAndIsOK()
	{
		$validateLoginInfo = new ValidateLoginInfo();

			if($this->CheckValidCookieTime())  // förutsätter att kakorna är satta..tiden är OK, så kollar vi validering.
			{
				if($validateLoginInfo->CookieLoginValidation( 
			    $_COOKIE["username"],$_COOKIE["userpassword"]))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			else
			{
				return false;	
			}
	
		
	}	
	
	/**
	 * @return boolean
	 */
	public function OnlyOneCookiesExists()
	{
		if(isset($_COOKIE['username']) && !isset($_COOKIE['userpassword']) ||
		  !isset($_COOKIE['username']) && isset($_COOKIE['userpassword']))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * @return boolean, false if cookie end time has been messed around with.
	 */
	public function CheckValidCookieTime()
	{
		$endTime= file_get_contents("endtime.txt");
		
		if(time() > $endTime)
		{ 
			return false;
		}
		else
		{
			return true;
		}
	}
	
	

	public function DestroyLoginCookies()
	{	
		setcookie("username", "", time()-3600);
		setcookie("userpassword","" ,time()-3600);
	}

	/**
	 * @Return string, username
	 */
	public function GetCookieUsername()
	{
		 
		if(isset($_COOKIE['username']))
		{
			return $_COOKIE['username'];
		}
		else
		{
			return "";
		} 
	}
	 
	 /**
	 * @Return string, userpassword
	 */
	public function GetCookieUserPassword()
	{
		
		if(isset($_COOKIE['userpassword']))
		{
			 return $_COOKIE['userpassword'];
		}
		else
		{
			return "";
		} 
	}
	
	
}

